home *** CD-ROM | disk | FTP | other *** search
Modula Implementation | 1995-03-19 | 7.3 KB | 228 lines |
- IMPLEMENTATION MODULE Files;
-
- (*
- Routines for finding information about files.
-
- Revision history:
- 0.00
-
-
- Copyright 1987 by:
- Dale W. Thompson, 14500 Dallas Pkwy. #2091, Dallas, TX 75240
-
- This module and/or its procedures may be freely used by anyone,
- but please acknowledge its use in any copyright notice of a
- publicly distributed program. Thank you.
-
- Please forward any comments, problems, or suggestions to me
- at the address given, or to my CompuServe ID 75115,734.
- *)
-
- FROM Dates IMPORT ParseDate;
- FROM DOSFiles IMPORT Open, Close, ModeOldFile, ModeNewFile, Read, Write,
- FileHandle, FileLock, Unlock, Lock,
- Examine, ExNext, AccessRead, FileInfoBlock,
- FileInfoBlockPtr, ParentDir;
- FROM Memory IMPORT AllocMem, FreeMem, MemClear, MemPublic, MemReqSet;
- FROM Strings IMPORT Length, Concat, Assign, Pos;
- FROM Strings2 IMPORT Equal;
- FROM SYSTEM IMPORT ADR, TSIZE, NULL;
-
- (*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*)
- PROCEDURE FileExists( VAR name: ARRAY OF CHAR ): BOOLEAN;
-
- VAR fl : FileLock;
-
- BEGIN
- fl := Lock( name, AccessRead );
- IF fl <> 0 THEN Unlock( fl ) END;
- RETURN fl <> 0;
- END (* PROCEDURE *) FileExists;
-
- (*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*)
- PROCEDURE CopyFile( VAR fileIn, fileOut : ARRAY OF CHAR ): BOOLEAN;
-
- CONST bufsize = 512;
-
- VAR
- hI, hO : FileHandle;
- readbytes, writbytes : LONGINT;
- copybuf : ARRAY [0..bufsize-1] OF CHAR;
- filecopied : BOOLEAN;
-
- BEGIN
- filecopied := FALSE;
- hI := Open( fileIn, ModeOldFile );
- IF hI # 0 THEN
- hO := Open( fileOut, ModeNewFile );
- IF hO # 0 THEN
- REPEAT
- readbytes := Read ( hI, ADR(copybuf), bufsize);
- writbytes := Write( hO, ADR(copybuf), LONGCARD(readbytes) );
- UNTIL readbytes < bufsize;
- Close(hO);
- filecopied := TRUE;
- END;
- Close(hI);
- END;
- RETURN filecopied;
- END (* PROCEDURE *) CopyFile;
-
- (*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*)
- PROCEDURE ReadDir( VAR Dir : ARRAY OF CHAR;
- VAR fia : ARRAY OF FileInfo ) : CARDINAL;
- (* returns number of files in directory *)
- VAR
- DirLock : FileLock;
- fibptr : FileInfoBlockPtr;
- i : CARDINAL;
-
- BEGIN
- i := 0;
- fibptr := AllocMem( LONGCARD(TSIZE(FileInfoBlock)),
- MemReqSet{MemPublic,MemClear} );
- IF fibptr # NULL THEN (* check for valid pointer *)
- DirLock := Lock( Dir, AccessRead );
- IF DirLock > 0 THEN (* check for successful lock *)
- IF Examine( DirLock, fibptr^ ) THEN
- Fillfi( fia[0], fibptr );
- WHILE ExNext( DirLock, fibptr^ ) & (i <= CARDINAL(HIGH(fia))) DO
- INC(i);
- Fillfi( fia[i], fibptr );
- END; (* WHILE *)
- END; (* IF Examine() *)
- Unlock( DirLock );
- END; (* IF DirLock > 0 *)
- FreeMem( fibptr, LONGCARD(TSIZE(FileInfoBlock)) );
- END; (* IF fibptr # NULL *)
- RETURN i; (* number of files in dir *)
- END (* PROCEDURE *) ReadDir;
-
-
- (*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*)
- PROCEDURE IsDirectory( VAR file : ARRAY OF CHAR ) : BOOLEAN;
-
- VAR fia : FileInfo;
-
- BEGIN
- IF GetFileInfo ( file, fia) THEN
- IF (fia.Type = Directory) THEN RETURN TRUE END;
- END;
- RETURN FALSE;
- END (* PROCEDURE *) IsDirectory;
-
-
- (*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*)
- PROCEDURE GetFileInfo( VAR file : ARRAY OF CHAR;
- VAR fia : FileInfo ) : BOOLEAN;
-
- VAR
- fl : FileLock;
- DirLock : FileLock;
- fibptr : FileInfoBlockPtr;
- i : BOOLEAN;
-
- BEGIN
- i := FALSE;
- fibptr := AllocMem( LONGCARD(TSIZE(FileInfoBlock)),
- MemReqSet{MemPublic,MemClear} );
- IF fibptr # NULL THEN (* check for valid pointer *)
- fl := Lock( file, AccessRead );
- IF fl > 0 THEN (* check for successful lock *)
- DirLock := ParentDir( fl );
- IF DirLock > 0 THEN (* check for successful lock *)
- IF Examine( DirLock, fibptr^ ) THEN
- LOOP
- IF NOT ExNext( DirLock, fibptr^ ) THEN EXIT END;
- Fillfi( fia, fibptr );
- IF Equal( file, fia.Name ) THEN i := TRUE; EXIT; END;
- END;
- END; (* IF Examine() *)
- Unlock( DirLock );
- END; (* IF DirLock > 0 *)
- Unlock( fl );
- END; (* IF fl > 0 *)
- END; (* IF fibptr # NULL *)
- FreeMem( fibptr, LONGCARD(TSIZE(FileInfoBlock)) );
- RETURN i; (* TRUE if FileInfo filled in *)
- END (* PROCEDURE *) GetFileInfo;
-
-
- (*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*)
- PROCEDURE Fillfi( VAR fia : FileInfo;
- fibptr : FileInfoBlockPtr );
-
- BEGIN
- Assign( fia.Name, fibptr^.fibFileName );
- ParseDate( fibptr^.fibDate, fia.Date );
- Assign( fia.Comment, fibptr^.fibComment );
- IF fibptr^.fibDirEntryType > 0 THEN
- fia.Type := Directory;
- ELSE
- fia.Type := File;
- END;
- fia.Size := fibptr^.fibSize;
- END (* PROCEDURE *) Fillfi;
-
- (*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*)
- PROCEDURE GetCurrentDir( VAR Path : ARRAY OF CHAR ): BOOLEAN;
-
- BEGIN
- RETURN GetSpec( CurrentDirLock(), Path )
- END (* PROCEDURE *) GetCurrentDir;
-
-
- (*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*)
- PROCEDURE GetSpec( lock : FileLock;
- VAR Path : ARRAY OF CHAR ): BOOLEAN;
-
- VAR
- i : CARDINAL;
- OldLock : FileLock;
- fibptr : FileInfoBlockPtr;
- temp : ARRAY [0..256] OF CHAR;
- status : BOOLEAN;
-
- BEGIN
- status := FALSE;
- Assign( temp, "" ); Assign( Path, "" );
- fibptr := AllocMem( LONGCARD(TSIZE(FileInfoBlock)),
- MemReqSet{MemPublic,MemClear} );
- IF fibptr # NULL THEN (* check for valid pointer *)
- LOOP
- IF lock > 0 THEN (* check for successful lock *)
- IF Examine( lock, fibptr^ ) THEN
- Concat( fibptr^.fibFileName, temp, Path );
- Concat( "/", Path, temp );
- OldLock := lock;
- ELSE
- EXIT;
- END; (* IF *)
- ELSE
- IF Pos( Path, "/", 0, i) THEN
- Path[i] := ":";
- ELSE
- Concat( Path, ":", Path );
- END; (* IF *)
- status := TRUE;
- EXIT;
- END; (* IF *)
- lock := ParentDir( OldLock );
- Unlock( OldLock );
- END; (* LOOP *)
- FreeMem( fibptr, LONGCARD(TSIZE(FileInfoBlock)) );
- END; (* if fibptr # NULL *)
- RETURN status;
- END (* PROCEDURE *) GetSpec;
-
-
- (*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*)
- PROCEDURE CurrentDirLock(): FileLock;
-
- BEGIN
- RETURN Lock("", AccessRead )
- END (* PROCEDURE *) CurrentDirLock;
-
-
- END (* IMPLEMENTATION MODULE *) Files.
-